package br.com.fiap.agenteDialogo.DrawGraph;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.apache.commons.collections15.Factory;
import org.apache.commons.collections15.Transformer;
import org.apache.commons.collections15.functors.MapTransformer;
import org.apache.commons.collections15.map.LazyMap;

import br.com.fiap.agenteDialogo.Beans.Edge;
import br.com.fiap.agenteDialogo.Beans.Vertex;
import edu.uci.ics.jung.algorithms.layout.AbstractLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.RenderContext;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.EditingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.EditingModalGraphMouse;
import edu.uci.ics.jung.visualization.control.EditingPopupGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.GraphMouseListener;
import edu.uci.ics.jung.visualization.control.GraphMousePlugin;
import edu.uci.ics.jung.visualization.control.LabelEditingGraphMousePlugin;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.control.MouseListenerTranslator;
import edu.uci.ics.jung.visualization.control.PluggableGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.picking.PickedState;
import edu.uci.ics.jung.visualization.renderers.VertexLabelRenderer;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Positioner;
import br.com.fiap.agenteDialogo.BLL.*;

public class DrawGraphApp extends JApplet {

	private static final long serialVersionUID = 1L;

	Graph<Vertex, Edge> graph;

	AbstractLayout<Vertex, Edge> layout;

	VisualizationViewer<Vertex, Edge> vv;

	public DrawGraphApp() {
		graph = new DirectedSparseMultigraph<Vertex, Edge>();

		this.layout = new StaticLayout<Vertex, Edge>(graph, new Dimension(600,600));

		vv = new VisualizationViewer<Vertex, Edge>(layout);

		vv.setBackground(Color.white);

		vv.getRenderContext().setEdgeLabelTransformer(MapTransformer.<Edge, String> getInstance(LazyMap.<Edge, String> decorate(new HashMap<Edge, String>(), new ToStringLabeller<Edge>())));

		vv.getRenderContext().setVertexLabelTransformer(MapTransformer.<Vertex, String> getInstance(LazyMap.<Vertex, String> decorate(new HashMap<Vertex, String>(), new ToStringLabeller<Vertex>())));

		Container content = getContentPane();

		final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);

		content.add(panel);
		
		Factory<Vertex> vertexFactory = new VertexFactory();

		Factory<Edge> edgeFactory = new EdgeFactory();

		final EditingModalGraphMouse<Vertex, Edge> graphMouse = new EditingModalGraphMouse<Vertex, Edge>(vv.getRenderContext(), vertexFactory, edgeFactory);

		vv.setVertexToolTipTransformer(vv.getRenderContext().getVertexLabelTransformer());

		vv.setGraphMouse(graphMouse);

		vv.addKeyListener(graphMouse.getModeKeyListener());

		graphMouse.setMode(ModalGraphMouse.Mode.EDITING);

		final JButton exportar = new JButton("Exportar para Xml");
		exportar.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {

				for (Vertex v : graph.getVertices()) {
					System.out.println(v.getValue());
				}

				actionPerformedExportar();
			}
		});

		final JButton editar = new JButton("Editar");
		editar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				if (editar.getText().equals("Editar")) {
					graphMouse.setMode(ModalGraphMouse.Mode.PICKING);
					editar.setText("Editando");
				} else {
					graphMouse.setMode(ModalGraphMouse.Mode.EDITING);
					editar.setText("Editar");
				}
			}
		});		

		JPanel controls = new JPanel();
		controls.add(editar);
		controls.add(exportar);

		content.add(controls, BorderLayout.SOUTH);
	}

	public static void main(String[] args) {
		final DrawGraphApp graphEditor = new DrawGraphApp();

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(graphEditor);
		frame.pack();
		frame.setVisible(true);
	}

	private void actionPerformedExportar() {
		JFileChooser chooser = new JFileChooser();
		int retrival = chooser.showSaveDialog(null);

		if (retrival == JFileChooser.APPROVE_OPTION) {
			boolean arquivoExportado = AgenteDialogoXml.exportarXml(graph,
					layout, chooser.getSelectedFile() + ".graphml");

			if (arquivoExportado) {
				JOptionPane.showMessageDialog(this,
						"Arquivo exportado com sucesso!");
			} else {
				JOptionPane.showMessageDialog(this,
						"Erro ao exportar o arquivo!");
			}
		}
	}

}
